package yxy.game.pm2.active.command.supfirst;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actsupfirstcharge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_超值首冲;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.PayMessage;
import yxy.game.pm2.message.PayMessage.SupFirstInfoRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@MessageType(proto = Command.SupFirstInfo, classtype = PayMessage.SupFirstInfoRequest.class)
public class CMD_Active_超值首充_Info extends Command<SupFirstInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_超值首充_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, SupFirstInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();

		PayMessage.SupFirstInfoResponse.Builder response = PayMessage.SupFirstInfoResponse.newBuilder();
		
		ActiveModule_超值首冲 am = ActiveModule_超值首冲.get(pcode,false);
		if(am == null) {
			response.setStart(0);
			response.setEnd(0);
			response.setPayamount(0);
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			response.setStart(0);
			response.setEnd(0);
			response.setPayamount(0);
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.haveopen(DateTime.now())) {
			response.setStart(0);
			response.setEnd(0);
			response.setPayamount(0);
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		response.setStart(am.getTimeline().getStartTime().getMillis());
		response.setEnd(am.getTimeline().getEndTime().getMillis());
		response.setPayamount(am.getPayamounts());
		
		HashMap<String, DateTime> gbtmap = am.getGroupbuytimemap();
		if(gbtmap == null) {
			gbtmap = new HashMap<String, DateTime>();
		}
		
		Map<String, actsupfirstcharge> map = ActDataCenter.getAllData(actsupfirstcharge.class, pcode);
		for(String key : map.keySet()) {
			actsupfirstcharge data = map.get(key);
			PayMessage.CzscInfo.Builder czscinfo = PayMessage.CzscInfo.newBuilder();
			czscinfo.setId(data.getId());
			czscinfo.setPaycondition(data.getChargecondition());
			czscinfo.setCondition(data.getCondition());
			czscinfo.setGroup(data.getGroup());
			czscinfo.setNum(data.getNum());
			czscinfo.setType(data.getType());
			czscinfo.setTitle(data.getTitle());
			
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			for(int j = 0; j < curs.size(); j++) {
//				PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
				Currency cur = curs.get(j);
	
				PackageMessage.Item item = new CurrencyHandle().packItem(cur);
//				item.setType(cur.getType().getNumber());
//				item.setDataId(cur.getDataId());
//				item.setNumber(cur.getNumber());
				czscinfo.addReward(item);
			}
			
//			czscinfo.setCanget(am.cannotget);
			int dataid = data.getId();
			int state = am.cangainreward(dataid, data);
			czscinfo.setCanget(state); //可领取的奖励 id
			response.addRewards(czscinfo);
		}
		
//		logger.info("CMD_Active_超值首充_Info" + response.getRewardsList().toString());
		send(messageSource, response, Result.Success);
	}

	
	private void send(MessageSource messageSource, PayMessage.SupFirstInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
